﻿using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using Portal.Core;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.Author"), Serializable, DataContractAttribute]
    public class Author : BaseObject
    {
        #region Ctor
        public Author()
        {
        }

        public Author(int id, string fullName)
        {
            ID = id;
            FullName = fullName;
        }
        #endregion

        #region Properties

        [SkipBinding]
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID { get; set; }

        [Column]
        public string FullName { get; set; }

        [Column]
        public string SortName { get; set; }    //to search

        [Column]
        public string SafeName { get; set; }    //to display url

        #endregion

        #region Extension Props
        public string Url
        {
            get
            {
                return string.Format("/tac-gia/{0}{1}", SafeName + "-" + ID, CoreContext.UrlExtension);
            }
        }
        public string DisplayFullName
        {
            get
            {
                return FullName ?? "[Không rõ]";
            }
        }
        #endregion

        #region Methods
        public static List<Author> All(bool renewCaching = false)
        {
            var parameter = new object[] { typeof(Author).Name, "All" };
            if (!renewCaching)
            {
                var obj = CacheHelper.Instance.Get(parameter);
                if (obj != null)
                    return (List<Author>)obj;
            }
            using (var context = new CmsDataContext())
            {
                var results = context.Authors.ToList().OrderBy(x => x.DisplayFullName).ToList();
                CacheHelper.Instance.Set(parameter, results);
                return results;
            }
        }

        public static List<Author> ListAll()
        {
            return All();
        }
        public static Author FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<Author>(objectid);
        }

        public static List<Author> FindBySortName(string sortName)
        {
            return ListAll().Where(x => ContainStr(x.SortName, sortName)).ToList();
        }
        public static Author MatchedBySortName(string sortName)
        {
            return ListAll().FirstOrDefault(x => EqualStr(x.SortName, sortName));
        }

        public static List<Author> Search(string keyword, int pageindex)
        {
            var sortkey = GetSortName(keyword);
            using (var context = new CmsDataContext())
            {
                var results = (string.IsNullOrEmpty(keyword)) ? context.Authors.ToList().OrderBy(x => x.FullName).Skip(pageindex * PageSize50).Take(PageSize50).ToList() :
                    context.Authors.Where(x => x.SortName.Contains(sortkey)).OrderBy(x => x.FullName).Skip(pageindex * PageSize50).Take(PageSize50).ToList();
                return results;
            }
        }
        public override void BeforeSaveUpdate()
        {
            base.BeforeSaveUpdate();
            SafeName = GetSafeName(FullName);
            SortName = GetSortNameFromSafeName(SafeName);
        }
        public override void AfterSaveUpdate()
        {
            base.AfterSaveUpdate();
            Recache(this);
        }
        public override void Delete()
        {
            //base.Delete();        //do not remove author!
        }
        public override void AfterDelete()
        {
            base.AfterDelete();
            Recache();
        }
        public static void Recache(Author obj = null)
        {
            if (obj != null)
                FindByPrimaryKey<Author>(obj.ID, true);

            All(true);
        }
        #endregion Methods
    }
    public class AuthorSearchObject
    {
        public string Character { get; set; }

        public override string ToString()
        {
            string value = Character;
            return value;
        }
    }
}
